/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_GFXFILTER_RENDER_TARGET_H_INCLUDED_
#define ALKES_GFXFILTER_RENDER_TARGET_H_INCLUDED_

#include "alkes/gfxfilter/internal.h"
#include "alkes/gfx/IRenderTarget3D.h"

namespace alkes {

/// \addtogroup gfxfilter
//@{

/** Decorator class of IGraphics3D interface.
*/
class AL_GF_EXPORT GfxFilterRenderTarget
    : public IRenderTarget3D
{
    AL_DECLARE_FILTER_CLASS(GfxFilterRenderTarget, IRenderTarget2D);

public:
    static GfxFilterRenderTarget* createRenderTarget2D(IGraphics3D* owner, IRenderTarget2D* target);
    static GfxFilterRenderTarget* createRenderTarget3D(IGraphics3D* owner, IRenderTarget3D* target);

    virtual void beginDraw2D();
    virtual void endDraw2D();

    virtual const Rect getTargetRect() const;

    virtual void setScissorRect(const Rect& rect);
    virtual const Rect getScissorRect();

    virtual void setState(int state, bool enable);
    virtual bool getState(int state);
    virtual void setAlphaBlendMode(AlphaBlendMode src_blend, AlphaBlendMode dst_blend);
    virtual void getAlphaBlendMode(AlphaBlendMode* src_blend, AlphaBlendMode* dst_blend);

    virtual void setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter);
    virtual void getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter);
    virtual void setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode);
    virtual void getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode);

    virtual bool fill(const Color& color, const Rect* rect = 0);

    virtual bool drawShape(Shape* shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(LineShape* line_shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(RectangleShape* rectangle_shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(ImageShape* image_shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(PolylineShape* polyline_shape, uint32_t start = 0, uint32_t count = 1);

    virtual void pushMatrix();
    virtual void popMatrix();
    virtual void translate(const Vector2D& offset);
    virtual void translate(float x, float y);
    virtual void scale(float xscale, float yscale, const Vector2D& origin = Vector2D::ZERO);
    virtual void scale(float scaling, const Vector2D& origin = Vector2D::ZERO);
    virtual void rotate(float angle, const Vector2D& origin = Vector2D::ZERO);
    virtual void skewX(float angle);
    virtual void skewY(float angle);


    // 3D interfaces
    virtual void beginDraw3D();
    virtual void endDraw3D();

    virtual void setViewport(const Viewport& viewport);
    virtual const Viewport getViewport() const;
    virtual void setPerspective(float angle, float aspect, float znear, float zfar);
    virtual void setLookAt(const Vector3D& eye, const Vector3D& at, const Vector3D& up);
    virtual void translate(const Vector3D& offset);
    virtual void translate(float x, float y, float z);
    virtual void scale(float xscale, float yscale, float zscale);
    virtual void rotate(float angle, float x, float y, float z);

    virtual void setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter, int stage);
    virtual void getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter, int stage);
    virtual void setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode, int stage);
    virtual void getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode, int stage);

    virtual void clearBuffer(uint32_t buffer_flag, const Color& clear_color, const Rect* rect = 0, float depth = 1.0f);
    virtual bool drawPrimitive(IVertexBuffer* vbuffer, PrimitiveType type, uint32_t start, uint32_t count);

    virtual bool setTexture(Image* image, int stage = 0);

protected:
    GfxFilterRenderTarget(IGraphics3D* owner, IRenderTarget3D* target);
    GfxFilterRenderTarget(IGraphics3D* owner, IRenderTarget2D* target);
    virtual ~GfxFilterRenderTarget();

//    virtual void onSetColor();
//    virtual void onSetViewport();

    virtual void destroy();
    virtual void onSnapshot(ImageWriter& writer, const Rect& rect);

    IRenderTarget2D* get2D() const;
    IRenderTarget3D* get3D() const;

private:
    bool is_3d_;
    intrusive_ptr<IRenderTarget2D> component_;
};

//@}

}

#endif
